const db = require('../db/index')
// const path = require('path')
const bcrypt = require('bcryptjs')

// 获取用户信息
exports.getUserInfo = (req, res) => {
    const id = req.user.id
    const sql = 'select id, userEmail, nickname, gender, avatar, introduce from user where id=?'
    db.query(sql, id, (err, results) => {
        if(err) return res.cc(err)

        if(results.length < 1) return res.cc('获取用户信息失败')

        res.send({
            status: 0,
            message: '获取用户信息成功',
            data: results[0]
        })
    })
}

// 修改用户信息
exports.updateInfo = (req,res) => {
    const sql = 'update user set ? where id=?'
    db.query(sql, [req.body, req.user.id], (err, results) => {
        if(err) return res.cc(err)
        if(results.affectedRows !== 1) return res.cc('修改信息失败')
        res.cc('修改信息成功', 0)
    })

}

// 修改密码
exports.changePwd = (req, res) => {
    const { oldPwd, newPwd } = req.body
    const selectSql = 'select password from user where id=?'
    db.query(selectSql, req.user.id, (err,results) => {
        if(err) return res.cc(err)

        if(results.length !== 1) return res.cc('用户不存在')

        // TODO1：判断密码是否正确----bcrypt.compareSync(用户提交的密码, 数据库的密码)
        const compareResults = bcrypt.compareSync(oldPwd, results[0].password)
        if(!compareResults) return res.cc('修改失败，原密码错误')

        // 对密码进行加密
        const password = bcrypt.hashSync(newPwd, 10)

        const sql = 'update user set password=? where id=?'
        db.query(sql, [password, req.user.id], (err, results) => {
            if(err) return res.cc(err)

            // console.log(results);
            if(results.affectedRows !== 1) return res.cc('修改密码失败')
            
            res.cc('修改密码成功',0)
        })
    })
}

// 上传头像
exports.uploadAvatar = (req, res) => {
    const sql = 'update user set avatar=? where id=?'
    // const avatar = path.join('../uploads', req.file.filename)
    db.query(sql, [req.body.avatar, req.user.id], (err, results) => {
        if(err) return res.cc(err)
        // console.log(results)
        if(results.affectedRows !== 1) return res.cc('上传头像失败')

        res.cc('上传头像成功', 0)
    })
}

// 获取地址
exports.getAddress = (req, res) => {
    const sql = 'select id, districts, city, county, full_address from address where user_id=? order by id desc'
    db.query(sql, req.user.id, (err, results) => {
        if(err) return res.cc(err)
        if(results.length < 1) return res.cc('没有数据',0)
        res.send({
            status: 0,
            message: '获取成功', 
            data: results
        })
    })
}

// 添加地址
exports.addAddress = (req, res) => {
    const {districts,city,county,full_address} = req.body
    const sql = 'insert into address set ?'
    db.query(sql, {districts, city,county, full_address, user_id: req.user.id}, (err, results) => {
        if(err) return res.cc(err)
        if(results.affectedRows !== 1 ) res.cc('添加失败')
        res.cc('添加成功', 0)
    })
}

// 修改地址
exports.updateAddress = (req, res) => {
    const sql = 'update address set ? where id=? and user_id = ?'
    db.query(sql,[req.body, req.body.id, req.user.id], (err, results) => {
        if(err) return res.cc(err)
        if(results.affectedRows !== 1 ) res.cc('修改失败')
        res.cc('修改成功', 0)
    })
}

// 删除地址
exports.deleteAddress = (req, res) => { 
    const sql = 'delete FROM address where id=?'
    db.query(sql, req.body.id, (err, results) => {
        if(err) return res.cc(err)
        if(results.affectedRows !== 1 )  res.cc('暂无数据',0)
        res.cc('删除成功', 0)
    })
}